package com.cby.android.sample.model

import android.app.Application
import com.cby.android.library.base.BaseStateViewModel
import com.cby.android.library.utils.DateUtils
import com.cby.android.sample.intent.WeatherIntent
import com.cby.android.sample.repository.CaiYunApiImpl
import com.cby.android.sample.repository.RollApiImpl
import com.cby.android.sample.repository.ZhiXinApiImpl
import com.cby.android.sample.state.WeatherState
import java.util.*

/**
 * @description: WeatherViewModel
 *
 * @author ChenBaoyang
 * @date 4/27/21 11:32
 */
class WeatherViewModel(application: Application) : BaseStateViewModel<WeatherState,WeatherIntent>(application) {


    private val rollApi = RollApiImpl()

    private val caiYunApi = CaiYunApiImpl()

    private val zhiXinApi = ZhiXinApiImpl()

    private fun loadWeather(date: String, location: String, cityName: String) {
        fetchDataWithNoStatus {
            showLoading()
            val weatherData: MutableList<Any> = ArrayList()
            val response1 = caiYunApi.getWeatherFromCaiyun(location)
            val response2 = rollApi.getCityWeatherFutureDays(cityName)
            val response3 = zhiXinApi.getLifeSuggestion(cityName)
            val response4 = rollApi.getHolidaySingle(date)
            weatherData.add(response1.result.realtime)
            weatherData.add(response2.data!!)
            weatherData.add(response3.results[0].suggestion)
            weatherData.add(response4.data!!)
            updateState(WeatherState(data = weatherData as ArrayList<Any>))
            showContent()
        }

    }

    private fun getHolidaySingle(date: String) {
        fetchDataWithStatus {
            val response = rollApi.getHolidaySingle(date)
            updateState(WeatherState(dataHoliday = response.data))
            response.data != null
        }
    }

    private fun getCityWeatherFutureDays(cityName: String) {
        fetchDataWithStatus {
            val response = rollApi.getCityWeatherFutureDays(cityName)
            updateState(WeatherState(dataForecast = response.data))
            response.data != null
        }
    }

    private fun getLifeSuggestion(cityName: String){
        fetchDataWithStatus {
            val response = zhiXinApi.getLifeSuggestion(cityName)
            updateState(WeatherState(dataLifeSuggestion = response.results[0]))
            true
        }
    }

    private fun getWeatherFromCaiYun(location: String){
        fetchDataWithStatus {
            val response = caiYunApi.getWeatherFromCaiyun(location)
            updateState(WeatherState(dataCaiYunWeather = response.result.realtime))
            true
        }
    }

    override fun dispatch(intent: WeatherIntent) {
        when (intent) {
            is WeatherIntent.LoadWeather -> loadWeather(DateUtils.getToday(), intent.location, intent.cityName)
            is WeatherIntent.LoadCaiYunWeather -> getWeatherFromCaiYun(intent.location)
            is WeatherIntent.LoadCityWeatherFutureDays -> getCityWeatherFutureDays(intent.cityName)
            is WeatherIntent.LoadHolidaySingle -> getHolidaySingle(DateUtils.getToday())
            is WeatherIntent.LoadLifeSuggestion -> getLifeSuggestion(intent.cityName)
        }
    }
}